// pages/life/add/add.js
const app = getApp()
import Notify from '@vant/weapp/notify/notify';
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		message: '',
		//存放全地址用于展示
		fileList: [],
		//存放文件名
		newArr: [],
		count: 1,
		index: '',
		dyn:{}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		var json = options.dynamic;
		var obj = JSON.parse(json)
		console.log(obj)
		this.setData({
			message: obj.body,
			dyn:obj
		})
		//给图片加上前缀，组成fileList
		let temp = []
		let arrTemp=[]
		let base_url = 'https://fileapi.wechat.copyctrl.top/data/upload/images/'
		obj.pic.forEach((item) => {
			if(item.length>3){
				arrTemp.push(item)
				item = base_url + item
				let index={url:''}
				index.url=item
				temp.push(index);
			}
		});
		this.setData({
			fileList:temp
		})
		console.log(this.data.fileList)
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		//渲染界面，先把传过来的数据赋值
	},
	//上传图片之前
	beforeRead(event) {
		const {
			file,
			callback
		} = event.detail;
		console.log(file)
		if (file.size > 10 * 1024 * 1024) {
			wx.showToast({
				title: '文件大小不能超过 10M!',
				icon: 'none'
			})
			callback(false);
		}
		let name = file.url.substring(file.url.lastIndexOf("."));
		if (name != '.png' && name != '.jpg') {
			wx.showToast({
				title: '图片必须是 .png 或 .jpg!',
				icon: 'none'
			})
			callback(false);
		}
		callback(true)
	},
	//点击删除图片
	del(event) {
		let id = event.detail.index //能获取到对应的下标
		let newArr = this.data.newArr //这里引用 上传的数组 
		let fileList = this.data.fileList //这里是前端页面展示的数组
		app.request({
			url: app.globalData.fileApi + '/dynamic',
			method: 'DELETE',
			data: {
				fileName: this.data.newArr[id],
			}
		}).then(res => {
			if (res.statusCode == 200) {
				newArr.splice(id, 1) //根据下标来删除对应的图片
				fileList.splice(id, 1)
				this.setData({
					fileList: fileList, //在这里进行重新赋值  删除后 图片剩几张就相当于给后台传几张
					newArr: newArr
				})
			} else {
				Notify('服务错误，稍后重试');
			}
		})
	},
	//上传图片
	afterRead(event) {
		let that = this
		var img = event.detail.file;
		// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
		wx.uploadFile({
			url: app.globalData.fileApi+'/dynamic',
			method: 'POST',
			filePath: img.url,
			name: 'file',
			formData: {
				user: 'test'
			},
			header: {
				"Content-Type": "multipart/form-data",
				"token": wx.getStorageSync('token')
			},
			success(res) {
				res.data = JSON.parse(res.data)
				that.data.newArr.push(res.data.data)
				that.setData({
					newArr: that.data.newArr
				})
				console.log(that.data.newArr)
			},
			fail(res) {
				console.log(res);
			}
		})
		this.data.fileList.push(img)
		that.setData({
			fileList: this.data.fileList
		})
	},
	submit() {
		var obj=this.data.dyn
		obj.boyd=this.data.message
		obj.pic=this.data.newArr
		var dynamic = JSON.stringify(obj)
		app.request({
			url: app.globalData.apiUrl + '/dynamic',
			data: {
				dynamic: dynamic
			},
			method: 'PUT',
		}).then(res => {
			if (res.statusCode == 200) {
				Notify({
          type: 'primary',
          message: '发布成功'
        })
				setTimeout(() => {
					// wx.navigateBack({
					// 	delta: 1,
					// 	success: function (e) {
					// 		var page = getCurrentPages().pop();
					// 		if (page == undefined || page == null) return;
					// 		page.onLoad();
					// 	}
					// })
					wx.navigateBack({
						delta: 1,
						success: function (e) {
							var page = getCurrentPages().pop();
							if (page == undefined || page == null) return;
							page.onLoad()
						}
					}) //返回上一个页面更新数据
				}, 1000);
			} else {
				Notify('服务错误，稍后重试');
			}
		})
	}
})